package edu.gatech.teamrocket;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;

import edu.gatech.teamrocket.models.Task;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

/**
 * Provides a list view row implementation with a checkbox for marking complete/incomplete.
 * @author Patrick
 *
 */
public class TaskArrayAdapter extends ArrayAdapter<Task> implements View.OnCreateContextMenuListener {
	private final List<Task> list;
	private final Context context;
	private DewApplication app;
	private Dao<Task, Integer> taskDao;

	/**
	 * Constructor takes in the context and the list of tasks to create an adapter for.
	 * @param context
	 * @param list of tasks
	 */
	public TaskArrayAdapter(Context context, List<Task> list) {  
		super(context, R.layout.rowbuttonlayout, list);
		app = (DewApplication) context.getApplicationContext();
		this.context = context;
		this.list = list;
		
		try {
			taskDao = app.getHelper().getTaskDao();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	static class ViewHolder {
		protected TextView text;
		protected TextView text2;
		protected CheckBox checkbox;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			LayoutInflater inflator = LayoutInflater.from(context);
			view = inflator.inflate(R.layout.rowbuttonlayout, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.text = (TextView) view.findViewById(R.id.label);
			viewHolder.text2 = (TextView) view.findViewById(R.id.label2);
			viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
			viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							Task element = (Task) viewHolder.checkbox
									.getTag();
							element.setCompleted(buttonView.isChecked());
							try {
								taskDao.update(element);
							} catch (SQLException e) {
								throw new RuntimeException(e);
							}
						}
					});
			view.setTag(viewHolder);
			viewHolder.checkbox.setTag(list.get(position));
		} else {
			view = convertView;
			((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
		}

		ViewHolder holder = (ViewHolder) view.getTag();
		holder.text.setText(list.get(position).getName());
		holder.text2.setText(list.get(position).getCategory().toString());
		holder.checkbox.setChecked(list.get(position).isCompleted());
		view.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				Intent i = new Intent(context.getApplicationContext(), ViewTaskActivity.class);
				i.putExtra("TASK_ID", list.get(position).getId());
				context.startActivity(i);
			}
		});
		
		view.setOnCreateContextMenuListener(null);
		return view;
	}
	
	public boolean deleteCompleted() throws SQLException {
		boolean result = false;
		for (Task task: list) {
			if (task.isCompleted()) {
				task.delete();
				result = true;
			}
		}
		
		return result;
	}
	
	public void deleteItem(int position) throws SQLException {
		taskDao.delete(list.get(position));
	}

	@Override
	public void onCreateContextMenu(ContextMenu arg0, View arg1,
			ContextMenuInfo arg2) {
		// TODO Auto-generated method stub
		
	}
}